const Node = require('./Node.js')
const Edge = require('./Edge.js')
const Graph = require('./Graph.js')

/**
 * 常见的图模板
 * @param {int[][]} matrix 
 * @returns Graph
 */
function createGraph(matrix) {
  graph = new Graph()
  for(let i=0; i<matrix.length; i++) {
    let weight = matrix[i][0]
    let from = matrix[i][1]
    let to = matrix[i][2]
    if(!graph.nodes.has(form)) {
      graph.nodes.set(form,new Node(from))
    }
    if(!graph.nodes.has(to)) {
      graph.nodes.set(to,new Node(to))
    }
    let fromNode = graph.nodes.get(from)
    let toNode = graph.nodes.get(to)
    let newEdge = new Edge(weight, fromNode, toNode)
    fromNode.nexts.push(toNode)
    fromNode.out++
    toNode.In++
    fromNode.edges.add(newEdge)
    graph.edges.add(newEdge)
  }
  return graph
}

module.exports = createGraph